<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\ChatLog;
use app\common\model\ChatMsg;
use app\common\model\Message;
use think\Db;

/***
 * 客户聊天接口
 */
class Chat extends Api
{

    /**
     * 我的消息列表
     */
    public function msgList(){
        $user_id = $this->userId;
        $data=[
            'uid'=>$user_id,
            'page'=>input('page',1),
            'limit'=>input('limit',20)
        ];
        $msg=new ChatMsg();
        $list=$msg->mgsList($data);
        return returnData(1,'success',$list);
    }

    /**
     * 获取对象消息数据
     */
    public function getMsgInfo(){
        $uid = $this->userId;
        $id=$this->request->param('id/d',0);
        if(empty($id)){
            return returnData(0,'获取失败');
        }
        $data=[
            'id'=>$id,
            'uid'=>$uid,
        ];
        $chat=new ChatLog();
        $info=$chat->mgsInfo($data);
        return returnData(1,'success',$info);
    }

    /**
     * 通知公告
     */
    public function notice(){
        $uid = $this->userId;
        $data=[
            'uid'=>$uid,
            'page'=>input('page',1),
            'limit'=>input('limit',20)
        ];
        $notice=new \app\common\model\Notice();
        $list=$notice->getNoticeList(1,'id','desc',$data['page'],$data['limit']);
        return returnData(1,'success',$list);
    }
    /**
     * 查看通知公告
     */
    public function viewNotice(){
        $id=$this->request->param('id/d',0);
        if(empty($id)){
            return returnData(0,'获取失败');
        }
        $notice=new \app\common\model\Notice();
        $info=$notice->getNoticeInfo($id);
        return returnData(1,'success',$info);
    }
    /**
     * 系统消息列表
     */
    public function message(){
        $uid = $this->userId;
        $data=[
            'uid'=>$uid,
            'page'=>input('page',1),
            'limit'=>input('limit',20)
        ];
        $msg=new Message();
        list($res,$list)=$msg->getList($data);
        if(!$res){
            return returnData(0,'获取失败');
        }
        return returnData(1,'success',$list);
    }
    /**
     * 最新消息列表
     */
    public function newMsg(){
        $uid = $this->userId;
        $msg=new Message();
        $info=$msg->hasNew($uid);
        return returnData(1,'success',$info);
    }
    /**
     * 查看消息详情
     */
    public function viewMsg(){
        $uid = $this->userId;
        $id=$this->request->param('id/d',0);
        if(empty($id)){
            return returnData(0,'获取失败');
        }
        $msg=new Message();
        $info=$msg->info($uid,$id);
        return returnData(1,'success',$info);
    }
    /**
     * 获取聊天记录
     */
    public function getChatList(){
        $uid = $this->userId;
        $id=$this->request->param('id/d',0);
        if(empty($id)){
            return returnData(0,'获取失败');
        }
        $data=[
            'id'=>$id,
            'uid'=>$uid,
            'page'=>input('page',1),
            'limit'=>input('limit',20)
        ];
        $chat=new ChatLog();
        $list=$chat->chatList($data);
        return returnData(1,'success',$list);
    }
    /**
     * 发送消息
     */
    public function sendChat(){
        $data=$this->request->param('msg');
        if(empty($data)){
            return returnData(0,'发送失败');
        }
        $chat=new ChatLog();
        list($res,$info)=$chat->addChat($data);
        if(!$res){
            return returnData(0,$info);
        }
        return returnData(1,'success',$info);
    }
}